package com.boardour.wifip2p.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;

import com.boardour.wifip2p.WifiP2pCommon;
import com.boardour.wifip2p.utils.LG;

public class ConnectionReceiver extends BroadcastReceiver {
    private IConnectListener connectListener;

    public ConnectionReceiver(IConnectListener connectListener) {
        this.connectListener = connectListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            switch (action) {
                // 用于指示 Wifi P2P 是否可用
                case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION:
                    int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
                    boolean isEnable = state == WifiP2pManager.WIFI_P2P_STATE_ENABLED;
                    LG.e("用于指示 Wifi P2P 是否可用:" + isEnable);
                    if (connectListener != null) {
                        connectListener.onWifiP2pStateChanged(isEnable);
                    }
                    break;
                // 对等节点列表发生了变化，一般调用discoverPeers后会回调
                case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION:
                    // 可以调用requestPeers不断更新Wifi列表
                    LG.e("WIFI_P2P_PEERS_CHANGED");
                    if (connectListener != null) {
                        connectListener.onPeersChanged();
                    }
                    break;

                // Wifi P2P 的连接状态发生了改变
                case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION:
                    NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
                    boolean isConnect = networkInfo.isConnected();
                    LG.e("是否连接p2p设备:" + isConnect);
                    if (connectListener != null) {
                        connectListener.onWifiP2pConnectionChanged(isConnect);
                    }
                    break;
                // 本设备的设备信息发生了变化
                case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION: {
                    LG.e("本设备的设备信息发生了变化");
                    if (connectListener != null) {
                        connectListener.onWifiP2pDeviceChanged((WifiP2pDevice) intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
                    }
                    break;
                }
            }
        }
    }

    public static IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        return intentFilter;
    }
}
